#!/usr/bin/perl

##### Stephen Tyree #####

use warnings;
use strict;

############ Variables #############
my $lowercase;
my $capital;
my $lowercase_plural;
my $capital_plural;
my $lowercase_possesive;
my $capital_possesive;
my $language_suffix;
my $filename;
my $inline1;
my $inline2;
my $line1;
my $line2;

########### Helper Functions #############
sub replace_group {
	my $line_to_replace = shift(@_);
	if (!$line_to_replace) {
		die "Called replace_group without an argument";
	}

	if ($line_to_replace =~ m/^msgid(?:_plural)? +(.*(?:g|G)roup.*)$/) {
		my $replacement_line = $1;

		##### mark organic group to stop it from being replaced #####
		$replacement_line =~ s/Organic Group/\@_ogcapcap/g;
		$replacement_line =~ s/Organic group/\@_ogcaplow/g;
		$replacement_line =~ s/organic group/\@_oglowlow/g;
		$replacement_line =~ s/Organic Groups/\@_ogccp/g;
		$replacement_line =~ s/Organic groups/\@_ogclp/g;
		$replacement_line =~ s/organic groups/\@_ogllp/g;

		##### mark words to be replaced (in case replacement words include "group" #####
		$replacement_line =~ s/ groups/ \@_lowerplural/g;
		$replacement_line =~ s/Groups /\@_capplural /g;
		$replacement_line =~ s/ group's/ \@_lowerpossesive/g;
		$replacement_line =~ s/Group's /\@_cappossesive /g;
		$replacement_line =~ s/ group/ \@_lowercase/g;
		$replacement_line =~ s/Group /\@_capital /g;
		$replacement_line =~ s/"groups"/"\@_lowerplural"/g;
		$replacement_line =~ s/"Groups"/"\@_capplural"/g;
		$replacement_line =~ s/"group's"/"\@_lowerpossesive"/g;
		$replacement_line =~ s/"Group's"/"\@_cappossesive"/g;
		$replacement_line =~ s/"group"/"\@_lowercase"/g;
		$replacement_line =~ s/"Group"/"\@_capital"/g;

		##### replaced marked words with their replacements #####
		$replacement_line =~ s/\@_lowerplural/$lowercase_plural/g;
		$replacement_line =~ s/\@_capplural/$capital_plural/g;
		$replacement_line =~ s/\@_lowerpossesive/$lowercase_possesive/g;
		$replacement_line =~ s/\@_cappossesive/$capital_possesive/g;
		$replacement_line =~ s/\@_lowercase/$lowercase/g;
		$replacement_line =~ s/\@_capital/$capital/g;

		##### put back organic groups #####
		$replacement_line =~ s/\@_ogcapcap/Organic Group/g;
		$replacement_line =~ s/\@_ogcaplow/Organic group/g;
		$replacement_line =~ s/\@_oglowlow/organic group/g;
		$replacement_line =~ s/\@_ogccp/Organic Groups/g;
		$replacement_line =~ s/\@_ogclp/Organic groups/g;
		$replacement_line =~ s/\@_ogllp/organic groups/g;

		##### Return msgid and msgstr line #####
		return $replacement_line;
	}
	##### Not a line to replace #####
	return $line_to_replace;
}

################### Input ####################
print "********************************************\n";
print "***Welcome to the OG nomenclature changer***\n";
print "********************************************\n";
print "-Use this program to change how groups are referred to in the OG module.\n";
print "-First need to ask a few questions about the word replacing \'group\'.\n\n";

print "How do you use the word in lowercase: ";
$lowercase = <STDIN>;
chomp($lowercase);

print "How do you use the word starting with a capital: ";
$capital = <STDIN>;
chomp($capital);

print "How do you use the word in lowercase but plural: ";
$lowercase_plural = <STDIN>;
chomp($lowercase_plural);

print "How do you use the word starting with a capital but plural: ";
$capital_plural = <STDIN>;
chomp($capital_plural);

print "How do you use the word in lowercase but possesive: ";
$lowercase_possesive = <STDIN>;
chomp($lowercase_possesive);

print "How do you use the word starting with a capital but possesive: ";
$capital_possesive = <STDIN>;
chomp($capital_possesive);

print "What would you like the language code to be (ex- english could be en): ";
$language_suffix = <STDIN>;
chomp($language_suffix);

print "\n";
print "Finished reading in variables.\n\n";

################### Script ########################

print "Begin Script...\n";

open(INFILE,"og.pot") or die "Unable to open og.pot to read. Are you in the right directory";
open(OUTFILE,">>".$language_suffix.".po") or die "Unable to open $language_suffix.po to write";
while ($inline1 = <INFILE>) {
	##### only concerned with msgid lines #####
	if ($inline1 =~ m/msgstr/) {
		next;
	}
	elsif ($inline1 !~ m/msgid/) {
		print OUTFILE $inline1;
		next;
	}

	###### look ahead one line to see what type of line it is #####
	$inline2 = <INFILE>;
	if ($inline2 =~ m/^ *msgid_plural/) {
		##### Replace if necessary #####
		$line1 = replace_group($inline1);

		##### If first line needed replacing, then second one does as well #####
		if ($line1 eq $inline1) {
			print OUTFILE $inline1 . $inline2 . "msgstr[0] \"\"\nmsgstr[1] \"\"\n";
		}
		else {
			$line2 = replace_group($inline2);
			print OUTFILE $inline1 . $inline2 . "msgstr[0] " . $line1 . "\nmsgstr[1] " . $line2 . "\n";
		}
	}
	elsif ($inline2 =~ m/^ *msgstr/) {
		##### Replace if necessary #####
		$line1 = replace_group($inline1);

		if ($line1 eq $inline1) {
			print OUTFILE $line1 . "msgstr \"\"\n";
		}
		else {
			print OUTFILE $inline1 . "msgstr " . $line1 . "\n";
		}
	}
	elsif ($line2 =~ m/^ *".*"/) {
		##### Need to replace arbitrary number of lines #####
		
	}
	else {
		print "Invalid og.pot. Didn't use the one provided?\n";
		exit(1);
	}
}

##### Cleaning Up #####
close INFILE;
close OUTFILE;

##### Finished #####
print "...Finished!\n"
